package net;

import javax.swing.*;
import java.io.*;
import java.net.ServerSocket;
import java.net.Socket;
import java.util.Scanner;

public class Server1 {
    public static void main(String[] args){
        try {
            ServerSocket server=new ServerSocket(9988);//实例化一个serversocket对象
            System.out.println("服务器已启动！");
            Socket socket=server.accept();//等待客户端请求
            System.out.println("与客服端建立连接！");
            String cip=socket.getInetAddress().getHostAddress();//获取客服的ip
            System.out.println("ip:"+cip);
            System.out.println("port:"+socket.getPort());//获取客户的端口
            Scanner kb=new Scanner(System.in);
            PrintWriter pw=new PrintWriter(socket.getOutputStream());
            String m=kb.nextLine();
            while (true){
                pw.write(m+"\n");
                pw.flush();
                m=kb.nextLine();
            }
//            kb.close();
        } catch (IOException e) {
//            e.printStackTrace();
            System.out.println("端口被占用！");
        }
    }
}
